﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using SalePartial.Model;
using SalePartial.BLL;
using SalePartial.Utility;

/// <summary>
/// ProcessFlowController 的摘要说明
/// </summary>
public class ProcessFlowController
{
    //login part
    public const string ACCOUNT_KEY = "ACCOUNT_KEY";
    public const string ACCOUNTTYPE_KEY = "ACCOUNTTYPE_KEY";
    public const string URL_PERSONAL_DEFAULT = "loginSucc.aspx?toUrl=";
    public const string URL_ENTERPRISE_DEFAULT = "loginSucc.aspx?toUrl=";
    public const string USRL_ADMIN_DEFAULT = "~/admin/index.aspx";
    public const string URL_FRONT_ERROR = "loginErr.aspx?toUrl=";
    public const string URL_ADMIN_ERROR = "~/admin/index.aspx";
    public const string URL_CREATEACCOUNT_SUCCESS = "~/regOk.aspx";
    public const string URL_INVALIDUSER = "/login.aspx";
    public const string URL_RELOGINADMIN = "~/admin/login.aspx";
    public const string URL_PUBEPRINFO_ERROR = "jump.aspx?id=@&url=eprBasic.aspx";  //'@'用来被替换
    public const string URL_PUBPRVINFO_ERROR = "jump.aspx?id=@&url=psnBasic.aspx";  //'@'用来被替换

    public const int ACCOUNT_NONE = 0;      //不存在的类型
    public const int ACCOUNT_PERSONAL = 1;   //个人会员
    public const int ACCOUNT_ENTERPRISE = 2; //企业会员
    public const int ACCOUNT_ADMIN = 3;      //管理员

    //功能模块定义
    public const int MODULE_PUBEPRINFO = 1;
    public const int MODULE_PUBNEEDINFO = 2;
    public const int MODULE_PUBPROVINFO = 3;

    public ProcessFlowController()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    //delete by jiang 20120527 start
    //public bool UpdatePassword(string userName, string orgPassword, string newPassword)
    //{
    //    SalePartial.BLL.User userBll = new SalePartial.BLL.User();
    //    ClsInterface api = new ClsInterface("./../api/pdo.config");
    //    bool bupdateSucc = false;

    //    int userType = GetCurUserType();
    //    switch (userType)
    //    {
    //        case ACCOUNT_PERSONAL:
    //            bupdateSucc = userBll.UpdatePersonalPassword(userName, orgPassword, newPassword);
    //            break;

    //        case ACCOUNT_ENTERPRISE:
    //            bupdateSucc = userBll.UpdateEnterprisePassword(userName, orgPassword, newPassword);
    //            break;

    //        case ACCOUNT_ADMIN:
    //            bupdateSucc = userBll.UpdateAdminPassword(userName, orgPassword, newPassword);
    //            break;

    //        default:
    //            break;
    //    }

    //    if (bupdateSucc)    //更新新的密码至session中
    //    {
    //        api.UpdatePassword(userName, newPassword);      //同步论坛中密码
    //        GetCurUserInfo().Password = newPassword;
    //    }

    //    return bupdateSucc;
    //}

    public bool ProcessLogin(string userName, string password, int userType, string toUrl)
    {
    //    SalePartial.BLL.User userBll = new SalePartial.BLL.User();
    //    SalePartial.BLL.BBSBll bbsBll = new SalePartial.BLL.BBSBll();
    //    SalePartial.Model.User userInfo = null;
    //    string urlSuccess = null;
    //    string urlFail = null;

    //    switch (userType)
    //    {
    //        case ACCOUNT_PERSONAL:
    //            userInfo = userBll.CheckPersonalLogin(userName, password);
    //            urlSuccess = URL_PERSONAL_DEFAULT + toUrl;
    //            urlFail = URL_FRONT_ERROR + toUrl;
    //            break;

    //        case ACCOUNT_ENTERPRISE:
    //            userInfo = userBll.CheckEnterpriseLogin(userName, password);
    //            urlSuccess = URL_ENTERPRISE_DEFAULT + toUrl;
    //            urlFail = URL_FRONT_ERROR + toUrl;
    //            break;

    //        case ACCOUNT_ADMIN:
    //            userInfo = userBll.CheckAdminLogin(userName, password);
    //            urlSuccess = USRL_ADMIN_DEFAULT;
    //            urlFail = URL_ADMIN_ERROR;
    //            break;

    //        default:
    //            break;
    //    }

    //    if (userInfo != null)
    //    {
    //        取金钱和积分信息
    //        /*SalePartial.Model.BBSUserInfo bbsUser = bbsBll.GetUserInfoByName(userName);
    //        if (bbsUser != null)
    //        {
    //            userInfo.UserEP = bbsUser.UserEP;
    //            userInfo.UserMoney = bbsUser.UserMoney;
    //        }*/

    //        userInfo.UserName = userName;
    //        userInfo.Password = password;
    //        HttpContext.Current.Session[ACCOUNT_KEY] = userInfo;
    //        HttpContext.Current.Session[ACCOUNTTYPE_KEY] = "" + userType;

    //         Determine where to redirect the user back too
    //         If they came in from the home page, take them to a similar page
    //        if (FormsAuthentication.GetRedirectUrl(userName, false).EndsWith(urlSuccess))
    //        {

    //            FormsAuthentication.SetAuthCookie(userName, false);
    //            HttpContext.Current.Response.Redirect(urlSuccess, true);

    //        }
    //        else
    //        {
    //             Take the customer back to where the came from
    //            FormsAuthentication.SetAuthCookie(userName, false);
    //            HttpContext.Current.Response.Redirect(FormsAuthentication.GetRedirectUrl(userName, false), true);
    //            HttpContext.Current.Response.Redirect(urlSuccess, true);
    //        }
    //    }
    //    else
    //    {
    //        if (userType != ACCOUNT_ADMIN)
    //        {
    //            HttpContext.Current.Response.Redirect(urlFail, true);
    //        }
    //    }

        return false;
    }

    //public bool CreateEnterpriseAccount(EnterpriseMember eprMem)
    //{
    //    bool retVal = false;
    //    SalePartial.BLL.Enterprise eprBll = new SalePartial.BLL.Enterprise();
    //    SalePartial.BLL.BBSUser abbsUser = new SalePartial.BLL.BBSUser();

    //    if (eprBll.AddEnterprise(eprMem))
    //    {
    //        调用API，在论坛中添加用户
    //        ClsInterface api = new ClsInterface("./api/pdo.config");
    //        api.RegUser(eprMem.UserName, eprMem.Password, 0, 0, "0", eprMem.Email, @"userface/1.gif", eprMem.Question,
    //            eprMem.Answer, "", "", "", "", "", 0, false, "");

    //        retVal = true;

    //        发送邮件
    //        Tools.SendRegisterMailToUser(eprMem.Email, eprMem.UserName, eprMem.Password, 2);

    //        注册用户session，并重定向
    //        HttpContext.Current.Session[ACCOUNT_KEY] = eprMem;
    //        HttpContext.Current.Session[ACCOUNTTYPE_KEY] = "" + ACCOUNT_ENTERPRISE;
    //        HttpContext.Current.Response.Redirect(URL_CREATEACCOUNT_SUCCESS, true);
    //    }

    //    return retVal;
    //}

    //public bool UpdateEnterpriseAccount(EnterpriseMember eprMem)
    //{
    //    bool retVal = false;
    //    SalePartial.BLL.Enterprise eprBll = new SalePartial.BLL.Enterprise();

    //    if (eprBll.UpdateEnterprise(eprMem))
    //    {
    //        retVal = true;
    //    }

    //    return retVal;
    //}

    //public bool CheckUserBasicInfo(int module)
    //{
    //    SalePartial.Model.User userInfo = GetCurUserInfo();
    //    string urlEprErr = URL_PUBEPRINFO_ERROR.Replace("@", "" + module);
    //    string urlPrvErr = URL_PUBPRVINFO_ERROR.Replace("@", "" + module);

    //    if (userInfo.RealName == null || userInfo.RealName.Length <= 0)   //基本信息还未填写，因为个人以及企业对于realname字段都必须填写
    //    {
    //        if (userInfo.TypeId == ACCOUNT_ENTERPRISE)
    //        {
    //            HttpContext.Current.Response.Redirect(urlEprErr, true);
    //        }
    //        else if (userInfo.TypeId == ACCOUNT_PERSONAL)
    //        {
    //            HttpContext.Current.Response.Redirect(urlPrvErr, true);
    //        }
    //        else
    //        {
    //            HttpContext.Current.Response.Redirect(URL_INVALIDUSER, true);
    //        }
    //    }

    //    return true;
    //}

    //public bool CreatePersonalAccount(PersonalMember personalMem)
    //{
    //    bool retVal = false;
    //    SalePartial.BLL.Personal personalBll = new SalePartial.BLL.Personal();
    //    SalePartial.BLL.BBSUser abbsUser = new SalePartial.BLL.BBSUser();

    //    if (personalBll.AddPersonal(personalMem))
    //    {

    //        ClsInterface api = new ClsInterface("./api/pdo.config");
    //        api.RegUser(personalMem.UserName, personalMem.Password, 0, 0, "0", personalMem.Email, @"userface/1.gif",
    //            personalMem.Question, personalMem.Answer, "", "", "", "", "", 0, false, "");

    //        retVal = true;

    //        发送邮件
    //        Tools.SendRegisterMailToUser(personalMem.Email, personalMem.UserName, personalMem.Password, 2);

    //        注册用户session，并重定向
    //        HttpContext.Current.Session[ACCOUNT_KEY] = personalMem;
    //        HttpContext.Current.Session[ACCOUNTTYPE_KEY] = "" + ACCOUNT_PERSONAL;
    //        HttpContext.Current.Response.Redirect(URL_CREATEACCOUNT_SUCCESS, true);
    //    }

    //    return retVal;
    //}

    //public bool UpdatePersonalAccount(PersonalMember personalMem)
    //{
    //    bool retVal = false;
    //    SalePartial.BLL.Personal personalBll = new SalePartial.BLL.Personal();
    //    ClsInterface api = new ClsInterface("./../api/pdo.config");

    //    if (personalBll.UpdatePersonal(personalMem))
    //    {
    //        api.UpdateUser(personalMem.UserName, personalMem.Password, "", personalMem.Email, "", personalMem.RealName,
    //            "%" + personalMem.Qq + "%" + personalMem.Msn + "%" + personalMem.Telphone + "%" + personalMem.MobileTel + "%" + personalMem.Address,
    //            "", "" + personalMem.Sex);
    //        retVal = true;
    //    }

    //    return retVal;
    //}

    //public bool UpdateEprPubStatus(EnterpriseMember eprMem)
    //{
    //    bool retVal = false;
    //    SalePartial.BLL.Enterprise eprBll = new SalePartial.BLL.Enterprise();
    //    ClsInterface api = new ClsInterface("./../api/pdo.config");

    //    if (eprBll.UpdateEprPublicByUserName(eprMem))
    //    {
    //        retVal = true;
    //    }

    //    return retVal;
    //}

    //public int GetCurUserType()
    //{
    //    if (HttpContext.Current.Session[ACCOUNTTYPE_KEY] == null)
    //    {
    //        return ACCOUNT_NONE;
    //    }
    //    else if (HttpContext.Current.Session[ACCOUNTTYPE_KEY].Equals("" + ACCOUNT_ENTERPRISE))
    //    {
    //        return ACCOUNT_ENTERPRISE;
    //    }
    //    else if (HttpContext.Current.Session[ACCOUNTTYPE_KEY].Equals("" + ACCOUNT_PERSONAL))
    //    {
    //        return ACCOUNT_PERSONAL;
    //    }
    //    else if (HttpContext.Current.Session[ACCOUNTTYPE_KEY].Equals("" + ACCOUNT_ADMIN))
    //    {
    //        return ACCOUNT_ADMIN;
    //    }

    //    return ACCOUNT_NONE;
    //}

    //public SalePartial.Model.User GetCurUserInfo()
    //{
    //    return (SalePartial.Model.User)(HttpContext.Current.Session[ACCOUNT_KEY]);
    //}
    //delete by jiang 20120527 end

    public void DestroyUserCount()
    {
        //注销用户session
        HttpContext.Current.Session[ACCOUNT_KEY] = null;
        HttpContext.Current.Session[ACCOUNTTYPE_KEY] = null;
    }
}
